package com.jingdong.controller;

import com.jingdong.model.Product;
import com.jingdong.model.Product_type_one;
import com.jingdong.model.Product_type_three;
import com.jingdong.model.Product_type_two;
import com.jingdong.service.product.ProductService;
import com.jingdong.service.productType.ProductTypeOneService;
import com.jingdong.service.productType.ProductTypeThreeService;
import com.jingdong.service.productType.ProductTypeTwoService;
import com.jingdong.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductTypeThreeService productTypeThreeService;
    @Autowired
    private ProductTypeOneService productTypeOneService;
    @Autowired
    private ProductTypeTwoService productTypeTwoService;

    @RequestMapping("productList")
    public ModelAndView productList(Long productTypeId,String productName){
        ModelAndView mav=new ModelAndView("product/productList");
        Map<String,String>map=new HashMap<String,String>();
        if (productName!=null&&!productName.equals("")) {
            String strProductName = null;
            try {
                strProductName = URLDecoder.decode(productName, "UTF-8");
                map.put("productName","%"+strProductName+"%");
                mav.addObject("strProductName",strProductName);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
         if (productTypeId!=null&&!productTypeId.equals("")){
            map.put("productTypeId",productTypeId+"");
        }
        List<ProductVo> voList=new ArrayList<ProductVo>();
        List<Product>productList=productService.productList(map);
        for (Product product:productList) {
            ProductVo vo=new ProductVo();
            vo.setId(product.getId());
            vo.setProduct_name(product.getProduct_name());
            vo.setPlace(product.getPlace());
            vo.setPrice(product.getPrice());
            vo.setResolution(product.getResolution());
            vo.setSize(product.getSize());
            vo.setType_id(product.getType_id());
            vo.setProductTypeName(productTypeThreeService.getById(product.getType_id()).getType_name_three());
            voList.add(vo);
        }
        mav.addObject("voList",voList);
        return mav;
    }
    @RequestMapping("toUpdate")
    public ModelAndView toUpdate(Integer id){
        Product product=productService.getById(id);
        System.out.println(product.getType_id()+"------------------------------------------");
        Product_type_three type_three= productTypeThreeService.getById(product.getType_id());
        Product_type_two type_two=productTypeTwoService.getById(type_three.getType_two_id());
        Product_type_one type_one=productTypeOneService.getById(type_two.getType_one_id());
        List<Product_type_one> typeOneList=productTypeOneService.oneTypeList();
        List<Product_type_two> type_twoList=productTypeTwoService.twoTypeList(type_one.getId().intValue());
        List<Product_type_three>type_threeList=productTypeThreeService.threeTypeList(type_two.getId().intValue());
        for (Product_type_three p:type_threeList) {
            System.out.println(p.getType_name_three());
        }
        ModelAndView mav=new ModelAndView("/product/productUpdate");
        mav.addObject("typeOneList",typeOneList);
        mav.addObject("typeOneId",type_one.getId());
        mav.addObject("typeTwoList",type_twoList);
        mav.addObject("typeTwoId",type_two.getId());
        mav.addObject("typeThreeList",type_threeList);
        mav.addObject("typeThreeId",type_three.getId());
        mav.addObject("product",product);
        return mav;
    }
    @RequestMapping("update")
    public ModelAndView update(Integer id,String productName,String place,String resolution,String size,Double price,Long typeId){
        Product product=productService.getById(id);
        product.setProduct_name(productName);
        product.setPlace(place);
        product.setSize(size);
        product.setResolution(resolution);
        product.setPrice(price);
        product.setType_id(typeId);
        product.setId(id.longValue());
        int updateResult=productService.update(product);
        if (updateResult>0){
            return productList(product.getType_id(),null);
        }
        return toUpdate(id);
    }
    @RequestMapping("delete")
    public ModelAndView delete(Integer id,Long typeId){
        int deleteResult=productService.delete(id);
        if(deleteResult>0){
            return productList(typeId,null);
        }
        return productList(typeId,null);
    }
}
